import React from 'react';
import {Spin, Card, Button, Table, Row, Col} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import PropTypes from 'prop-types';
import routes from './routes';
import {TABLE} from '../constants';
import {PERMISSION} from './constants';
import {getDetail, abandonData} from './api';
import {injectIntl} from './intl';
import {hasPermissions} from './utils';
import WrappedPopconfirm from '../common/WrappedPopconfirm';
import styles from './style.css';
const {Description} = DescriptionList;
class DetailPanel extends React.PureComponent {
    state = {
        isFetching: false,
        data: {},
        details: []
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({isFetching: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        isFetching: false,
                        data: res.data,
                        details: res.data.details
                    });
                else
                    this.setState(({
                        isFetching: false
                    }));
            });
    }
    onCancel = () => {
        this.props.history.push(routes.query.url());
    }
    onClickAbandon = () => {
        this.setState({loading: true});
        const data = {
            rowVersion: this.state.data.rowVersion
        };
        abandonData(this.state.data.id, data)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    render() {
        const {data, details} = this.state;
        const {formatMessage} = this.props.intl;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.update) && hasPermissions(data.options, PERMISSION.update))
            operations.push(<Button key="update" href={`#${routes.update.format(this.props.id)}`} size="small">{
                formatMessage({
                    id: 'detailPanel.update',
                    defaultMessage: '编辑'
                })
            }</Button>);
        if(hasPermissions(this.props.permissions, PERMISSION.abandon) && hasPermissions(data.options, PERMISSION.abandon))
            operations.push(<WrappedPopconfirm
                key="abandon"
                id={this.props.id}
                placement="topLeft"
                onConfirm={this.onClickAbandon}
                title={formatMessage({
                    id: 'detailPanel.validate.abandon',
                    defaultMessage: '是否确认作废？'
                })}>
                <Button key="abandon" size="small">{
                    formatMessage({
                        id: 'detailPanel.abandon',
                        defaultMessage: '作废'
                    })
                }</Button>
            </WrappedPopconfirm>);
        const colums = [{
            title: formatMessage({
                id: 'detailPanel.productCategeryCode',
                defaultMessage: '车型编号'
            }),
            dataIndex: 'productCategeryCode'
        }];
        return (
            <div>
                <Spin spinning={this.state.isFetching}>
                    <Card>
                        <DescriptionList size="large" title={<Row><Col span={12}>{
                            formatMessage({
                                id: 'detailPage.card.common',
                                defaultMessage: '基本信息'
                            })
                        }</Col>
                        <Col className={`col-align-right ${styles.detail}`} span={12}>{operations}</Col>
                        </Row>}>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.laborFee',
                                defaultMessage: '编号'
                            })}>{data.code}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.brandName',
                                defaultMessage: '品牌'
                            })}>{`(${data.brandCode})${data.brandName}`}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.dutyUnitCode',
                                defaultMessage: '供应商编号'
                            })}>{data.dutyUnitCode}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.dutyUnitName',
                                defaultMessage: '供应商名称'
                            })}>{data.dutyUnitName}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.partCode',
                                defaultMessage: '原备件编号'
                            })}>{data.partCode}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.partName',
                                defaultMessage: '原备件名称'
                            })}>{data.partName}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.replacedPartCode',
                                defaultMessage: '替换备件编号'
                            })}>{data.replacedPartCode}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.replacedPartName',
                                defaultMessage: '替换备件名称'
                            })}>{data.replacedPartName}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.remark',
                                defaultMessage: '备注'
                            })}>{data.remark}</Description>
                        </DescriptionList>
                    </Card>
                    <Card title={formatMessage({
                        id: 'detailPanel.card.detail',
                        defaultMessage: '不供车型清单'
                    })}>
                        <Table
                            className="white-space-nowrap"
                            columns={colums}
                            rowKey="productCategeryId"
                            pagination={false}
                            dataSource={details}
                            {...TABLE} />
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" onClick={this.onCancel}>{this.props.intl.formatMessage({
                        id: 'detailPanel.operation.onCancel',
                        defaultMessage: '返回'
                    })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object
};

import {connect} from 'react-redux';
import {tableSearch} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = (state, props) => ({
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
